package com.peace.hospital.mapper.publicn;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.peace.hospital.entity.common.Druginformation;
import com.peace.hospital.entity.drugstorage.Outdrug;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.jdbc.core.ColumnMapRowMapper;

/**
 * <p>
 * 药品信息表 Mapper 接口
 * </p>
 *
 * @author xie
 * @since 2019-11-13
 */
@Mapper
public interface DruginformationMapper extends BaseMapper<Druginformation> {


    //添加药瓶信息
    @Insert("insert into druginformation\n" +
            "(druid,drugname,drugnumber,drugdate,drugdbsource,dugperiod)\n" +
            "values(#{druid},#{drugname},#{drugnumber},#{drugdate},#{drugdbsource},#{dugperiod})")
    public int addDrug(Druginformation din);


//查询药品信息
    @Select("select drugid,druid,phaid,drugname,drugnumber,drugdate,dugperiod,dugperiod,drugplidate,drugplival,drugprice,prid\n"+
            "from hospital_public.druginformation\n"+
            "where drugid=#{drugid} or drugname=#{drugname}")
    @Results({
            @Result(column="phaid",property="pharmacy",one=@One(select="com.peace.hospital.mapper.drugstorage.PharmacyMapper.findPh",fetchType= FetchType.LAZY)),
            @Result(column="druid",property="drugcategory",one=@One(select="com.peace.hospital.mapper.publicn.DrugcategoryMapper.findDca",fetchType= FetchType.LAZY)),
            @Result(column="prid",property="price",one=@One(select="com.peace.hospital.mapper.publicn.PriceMapper.finPrice",fetchType= FetchType.LAZY)),
    })
    public Druginformation findDrug(Druginformation din);

    //更新药品信息
    @Update("update hospital_public.druginformation\n" +
            "set druid=#{druid},\n" +
            "phaid=#{phaid},\n" +
            "drugnumber=#{drugnumber},\n" +
            "drugdate=#{drugdate},\n" +
            "dugperiod=#{dugperiod},\n" +
            "drugplidate=#{drugplidate},\n" +
            "drugplival=#{drugplival},\n" +
            "drugprice=#{drugprice},\n" +
            "prid=#{prid}\n" +
            "where drugid=#{drugid}")
    public int updateDrug(Druginformation din);


//  查询药品信息最后一一条记录id
    @Select("select drugid from druginformation order by drugid desc limit 1")
    public int findDruginLastId();

//    根据id查询药品信息
    @Select("select druid,phaid,drugname,drugnumber,drugdate,dugperiod,drugplidate,drugplival,drugprice,prid from druginformation where drugid=#{drugid}")
    @Results({
            @Result(column="phaid",property="pharmacy",one=@One(select="com.peace.hospital.mapper.drugstorage.PharmacyMapper.findPh",fetchType= FetchType.LAZY)),
            @Result(column="druid",property="drugcategory",one=@One(select="com.peace.hospital.mapper.publicn.DrugcategoryMapper.findDca",fetchType= FetchType.LAZY)),
            @Result(column="prid",property="price",one=@One(select="com.peace.hospital.mapper.publicn.PriceMapper.finPrice",fetchType= FetchType.LAZY)),
    })
    public Druginformation selectById(int drugid);

//        //    出库时删除入库药品信息
    @Update("update hospital_public.druginformation set drugnumber=drugnumber-#{outdnum} where drugid=#{drugid}")
    public int updateDruginfo(Outdrug outdrug);
    @Select("select phaid,drugname,drugnumber,drugdate,dugperiod,drugplidate,drugplival,drugprice,prid from druginformation where drugid=#{drugid}")
    public Druginformation selectByDrugId(int drugid);
}
